﻿using System;

namespace LightSwitchApplication
{
    public class Utils
    {
        public static string SimpleEncoder(string plain)
        {
            if (!string.IsNullOrEmpty(plain))
            {
                byte[] byt = System.Text.Encoding.UTF8.GetBytes(plain);
                // convert the byte array to a Base64 string
                plain = Convert.ToBase64String(byt);
            }
            else
            {
                plain = string.Empty;
            }
            return plain;
        }

        public static string SimpleDecoder(string encoded, string terminator="")
        {
            if (!string.IsNullOrEmpty(encoded))
            {
                byte[] b = Convert.FromBase64String(encoded.Replace(' ', '+') + terminator); // + "=="
                string strOriginal = System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
                return strOriginal;
            }
            else
            {
                return string.Empty;
            }
        }

        //private static Uri GetBaseAddress()
        //{
        //    // Get the web address of the .xap that launched this application
        //    string strBaseWebAddress = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri;
        //    // Find the position of the ClientBin directory        
        //    int PositionOfClientBin =
        //        System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri.ToLower().IndexOf(@"/default");
        //    // Strip off everything after the ClientBin directory         
        //    strBaseWebAddress = Microsoft.VisualBasic.Strings.Left(strBaseWebAddress, PositionOfClientBin);
        //    // Create a URI
        //    Uri UriWebService = new Uri(String.Format(@"{0}", strBaseWebAddress));
        //    // Return the base address          
        //    return UriWebService;
        //}
    }
}
